package com.kajespero.domain.commons.impl;

import java.lang.reflect.ParameterizedType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kajespero.dao.commons.DAO;
import com.kajespero.domain.commons.Domain;
import com.kajespero.vo.VO;

public abstract class AbstractDomain<D extends DAO<T>, T extends VO> implements Domain<D, T> {
	
	private static final Log LOG = LogFactory.getLog(AbstractDomain.class);
	
	private D dao;

	@Override
	public D getDAO() {
		if(dao == null){
			initDAO();
		}
		return dao;
	}
	

	@SuppressWarnings("unchecked")
	private void initDAO(){
		ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
		LOG.debug("class test == " + getClass());
		Class<D> clazz = (Class<D>) parameterizedType.getActualTypeArguments()[0];
		try {
			dao = clazz.newInstance();
		} catch (InstantiationException e) {
			LOG.error("InstantiationException " + e.getMessage(), e);
		} catch (IllegalAccessException e) {
			LOG.error("IllegalAccessException " + e.getMessage(), e);
		}
	}
}
